home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Dema / betonsoldier_spdemo.exe / {app} / Shaders / lighting.fx < prev    next >
Encoding:
Text File  |  2005-05-05  |  5.5 KB  |  120 lines

  1. //------------------------------------------------------------------------------------------------------
  2. //------------------------------------------------------------------------------------------------------
  3. //------------------------------------------------------------------------------------------------------
  4.  
  5. // Binormal computation without mirroring test
  6. inline float3    ComputeBinormal    (const float3 bumpTangent, const float3 bumpNormal);
  7.  
  8. // Binormal computation with mirroring test
  9. inline float3    ComputeBinormal    (const float3 bumpTangent, const float3 bumpNormal, const float3 normal);
  10.  
  11. // Vector computation and normalization
  12. inline float3    ComputeVector (const float3 position, const float3 destination);
  13.  
  14. // Vector computation, normalization and length
  15. inline float4    ComputeVectorAndLength (const float3 position, const float3 destination);
  16.  
  17. // Transform a vertor in texture space
  18. inline float3    TextureSpaceTransform (const float3 inputVector, const float3 bumpTangent, const float3 bumpNormal, const float3 bumpBinormal);
  19.  
  20. // Half View vector computation and normalization
  21. inline float3    ComputeHalfViewVector (const float3 lightDirection, const float3 cameraDirection);
  22.  
  23. // Compute attenuation
  24. inline float    ComputeAttenuation (const float distance, const float attenuationStart, const float attenuationEnd, const float attenuationDelta);
  25.  
  26. //------------------------------------------------------------------------------------------------------
  27. //------------------------------------------------------------------------------------------------------
  28. //------------------------------------------------------------------------------------------------------
  29.  
  30. inline float3    ComputeBinormal    (const float3 bumpTangent, const float3 bumpNormal)
  31. {
  32.     // binormal computation based on input bumb tangent and bump normal
  33.     return  cross (bumpTangent,bumpNormal);
  34. }
  35.  
  36. //------------------------------------------------------------------------------------------------------
  37.  
  38. inline float3    ComputeBinormal    (const float3 bumpTangent, const float3 bumpNormal, const float3 normal)
  39. {
  40.     // binormal computation based on input bumb tangent and bump normal
  41.     float3 binormal = cross (bumpTangent,bumpNormal);
  42.     
  43.     // if dot product between binormal and normal is negative, invert the binormal
  44.     float binormalDotNormal = dot (binormal,normal);
  45.     float3 signe = sign (binormalDotNormal);
  46.     binormal *= signe;
  47.     
  48.     return binormal;
  49. }
  50.  
  51. //------------------------------------------------------------------------------------------------------
  52. //------------------------------------------------------------------------------------------------------
  53. //------------------------------------------------------------------------------------------------------
  54.  
  55. inline float3    ComputeVector (const float3 position, const float3 destination)
  56. {
  57.     float3 direction = position - destination;
  58.     return  normalize(direction);    
  59. }
  60.  
  61. //------------------------------------------------------------------------------------------------------
  62.  
  63. inline float4    ComputeVectorAndLength (const float3 position, const float3 destination)
  64. {
  65.     float4 direction;
  66.     
  67.     direction.xyz = position - destination;
  68.     direction.w = direction.x * direction.x + direction.y * direction.y + direction.z * direction.z;
  69.     direction.xyz *= rsqrt(direction.w);
  70.         
  71.     return  direction;    
  72. }
  73.  
  74. //------------------------------------------------------------------------------------------------------
  75. //------------------------------------------------------------------------------------------------------
  76. //------------------------------------------------------------------------------------------------------
  77.  
  78. inline float3    TextureSpaceTransform (const float3 inputVector, const float3 bumpTangent, const float3 bumpNormal, const float3 bumpBinormal)
  79. {
  80.     float3 toTextureSpace;
  81.     
  82.     toTextureSpace.x = dot (inputVector,bumpTangent);
  83.     toTextureSpace.y = dot (inputVector,bumpNormal);
  84.     //toTextureSpace.y = dot (inputVector,-bumpNormal);
  85.     toTextureSpace.z = dot (inputVector,bumpBinormal);
  86.     
  87.     return toTextureSpace;
  88. }
  89.  
  90. //------------------------------------------------------------------------------------------------------
  91. //------------------------------------------------------------------------------------------------------
  92. //------------------------------------------------------------------------------------------------------
  93.  
  94. inline float3    ComputeHalfViewVector (const float3 lightDirection, const float3 cameraDirection)
  95. {
  96.     float3 halfView = lightDirection + cameraDirection;
  97.  
  98.     return normalize(halfView);
  99. }
  100.  
  101. //------------------------------------------------------------------------------------------------------
  102. //------------------------------------------------------------------------------------------------------
  103. //------------------------------------------------------------------------------------------------------
  104.  
  105. inline float    ComputeAttenuation (const float distance, const float attenuationStart, const float attenuationEnd, const float attenuationDelta)
  106. {
  107.     float attenuation;
  108.     
  109.     attenuation = max (distance,attenuationStart);
  110.     attenuation = attenuationEnd - attenuation;
  111.     attenuation = max (attenuation,0.0f);
  112.     attenuation *= attenuationDelta;
  113.                 
  114.     return attenuation;
  115. }
  116.  
  117. //------------------------------------------------------------------------------------------------------
  118. //------------------------------------------------------------------------------------------------------
  119. //------------------------------------------------------------------------------------------------------
  120.